package src

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func BindJson(ctx *gin.Context) {

	ctx.HTML(http.StatusOK, "chapter/bindjson.html", nil)
}

func BindJsonPost(ctx *gin.Context) {

	var user User

	err := ctx.ShouldBind(&user)
	fmt.Println(user)
	fmt.Printf("Type of user: %T\n", user)
	fmt.Println(err)

	if err != nil {

		ctx.JSON(http.StatusOK, gin.H{
			"code": http.StatusNotFound,
			"data": gin.H{
				"name": user.Name,
				"age":  user.Age,
				"addr": user.Addr,
			},
			"msg": "绑定失败",
		})
	} else {
		ctx.JSON(http.StatusOK, gin.H{
			"code": http.StatusOK,
			"msg":  "成功!",
		})
	}

}
